import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/ui/widget/common_product_vertical_widget.dart';
import 'package:tin_flutter/ui/widget/common_title_normal_widget.dart';

import '../../../../../app/res/app_color.dart';
import '../../../../../app/res/intl.dart';
import '../../main_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/11/4
/// * 时间: 16:42
/// * 描述: 猜你喜欢专区
class RecommendWidget extends StatefulWidget {
  final ScrollController scrollController;

  const RecommendWidget({Key? key, required this.scrollController}) : super(key: key);

  @override
  State<RecommendWidget> createState() => _RecommendWidgetState();
}

class _RecommendWidgetState extends State<RecommendWidget> {
  final logic = Get.find<MainLogic>();

  @override
  void initState() {
    super.initState();
    widget.scrollController.addListener(_scrollListener);
    logic.getRecommendProduct(isRefresh: true);
  }

  void _scrollListener() {
    // 当滚动到距离底部一定距离时触发加载
    if (widget.scrollController.position.extentAfter < 10 &&
        !logic.state.isLoading.value &&
        widget.scrollController.position.userScrollDirection == ScrollDirection.reverse) {
      logic.onLoadMore();
    }
  }

  @override
  void dispose() {
    widget.scrollController.removeListener(_scrollListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Padding(
        padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
        child: Container(
          decoration: BoxDecoration(
            color: AppColor.white,
            borderRadius: BorderRadius.all(Radius.circular(5.0)),
          ),
          child: Padding(
            padding: EdgeInsets.all(12.0),
            child: Column(
              children: [
                CommonTitleNormalWidget(
                    imageUrl: 'assets/images/icon_recommend_product.png',
                    title: Intl().likeTitle,
                    content: Intl().likeContent),
                SizedBox(height: 5.0),
                GridView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 5.0,
                    crossAxisSpacing: 5.0,
                    childAspectRatio: 0.7,
                  ),
                  itemCount: logic.state.recommendProductList.length,
                  // 为加载指示器预留位置
                  itemBuilder: (context, index) {
                    return CommonProductVerticalWidget(
                        imageUrl: logic.state.recommendProductList[index].pic,
                        title: logic.state.recommendProductList[index].name,
                        content: logic.state.recommendProductList[index].subTitle,
                        price: logic.state.recommendProductList[index].price.toInt());
                  },
                ),
                SizedBox(height: 8.0),
                Text(
                  !logic.state.hasMore.value
                      ? '没有更多数据了'
                      : (logic.state.isLoading.value ? '正在加载...' : '上拉加载更多'),
                  style: TextStyle(fontSize: 12.0, color: AppColor.grey500),
                )
              ],
            ),
          ),
        ),
      );
    });
  }
}
